//3.状态模式典型应用之“promise”

class Promise{
    constructor(fn){
        this.state = 'inital';
        this.success = [];
        this.error = [];
        let resolve = (data)=>{
            this.state = 'fulfilled';
            this.success.forEach(item=>item(data));
        };
        let reject = (err)=>{
            this.state = 'failed';
            this.error.forEach(item=>item(err));
        };
        fn(resolve,reject);
    };
    then(success,error){
        this.success.push(success);
        this.error.push(error);
    }
};
new Promise(function (resolve,reject) { 
    setTimeout(function () { 
        resolve('ok');
     },1000)
 }).then(res=>{
     console.log(res)
 })